#include "StdAfx.h"
#include "GameBase.h"
#include "SubStructure.h"
#include "Effect.h"

Effect::Effect()
{
	nowAniState1 = 0;
	nowAniState2 = 0;
	nowAniCount = 0;
	nowMoveState1 = 0;
	nowMoveState2 = 0;
	nowMoveCount = 0;

	scaleratio = 0.0f;

	radian = 0.0f;
	radianvel = 0.0f;
	
	nowState = LIVEON;
	nowStateCount = 0;

	liveTime = 0;
	limitedTime = 120;
}

Effect::~Effect()
{
	for(int i = 0; i < _Ani.size(); ++i)
	{
		for(int j = 0; j < _Ani[i].size(); ++j)
		{
			delete _Ani[i][j];
		}
	}
	
	for(int i = 0; i < _Move.size(); ++i)
	{
		for(int j = 0; j < _Move[i].size(); ++j)
		{
			delete _Move[i][j];
		}
	}
}

void Effect::Draw()
{
	if(nowState == NONLIVE)
		return;
	D3DXVECTOR2 imgcenter((nowAni.Source.bottom - nowAni.Source.top)/2.0,
		(nowAni.Source.right - nowAni.Source.left)/2.0);
	
	D3DXMATRIX matrix;
	D3DXMatrixIdentity(&matrix);

	D3DXMatrixTransformation2D(&matrix, &imgcenter, scaleratio, &Scalingval, &imgcenter, radian - M_PI_2, &this_pos);

	GameBase::pSprite->SetTransform(&matrix);
	GameBase::pSprite->Draw(nowAni.Texture,
					&nowAni.Source,
					NULL,
					NULL,
					nowAni.invisible);

	D3DXMatrixIdentity(&matrix);
	GameBase::pSprite->SetTransform(&matrix);
}

void Effect::Update()
{
	if(nowState == NONLIVE)
		return;
	nowAniCount++;
	if(nowAniCount >= nowAni.this_cut_count)
	{
		if(nowAniState2 >= (_Ani[nowAniState1].size() - 1))
		{
			nowAniState1 = nowAni.next_ani;
			nowAniState2 = 0;
			nowAni = *_Ani[nowAniState1][nowAniState2];
		}
		else
		{
			++nowAniState2;
			nowAni = *_Ani[nowAniState1][nowAniState2];
		}
		nowAniCount = 0;
	}
	Scalingval += Scalingvelo;
	if(Scalingval.x > LimitScaling.x)
	{
		Scalingval.x = LimitScaling.x;
	}
	else if(Scalingval.x < 0.0f)
	{
		Scalingvelo.x = -Scalingvelo.x;
	}
	if(Scalingval.y > LimitScaling.y)
	{
		Scalingval.y = LimitScaling.y;
	}
	else if(Scalingval.y < 0.0f)
	{
		Scalingvelo.y = -Scalingvelo.y;
	}

	radian += radianvel;
	if(radian > M_PI * 2.0f)
	{
		radian -= M_PI * 2.0f;
	}
	if(radian < 0.0f)
	{
		radian += M_PI * 2.0f;
	}

	liveTime++;
}

void Effect::Move()
{
	if(nowState == NONLIVE)
		return;
	this_pos += nowMove.Move_Vel;
	nowMove.Move_Vel += nowMove.Move_Acc;
	nowMoveCount++;
	if(nowMoveCount > nowMove.move_count)
	{
		if(nowMoveState2 >= (_Move[nowMoveState1].size() - 1))
		{
			nowMoveState1 = nowMove.next_Move;
			nowMoveState2 = 0;
			nowMove = *_Move[nowMoveState1][nowMoveState2];
		}
		else
		{
			++nowMoveState2;
			nowMove = *_Move[nowMoveState1][nowMoveState2];
		}
		nowMoveCount = 0;
	}
}

void Effect::Reset()
{
}

void Effect::SetAni(AniVec2 nA)
{
	_Ani = nA;
}

void Effect::SetMove(Move_Vec2 nM)
{
	_Move = nM;
}

void Effect::SetNowAniState(DWORD ns)
{
	nowAniState1 = ns;
	nowAniState2 = 0;
	nowAni = *_Ani[nowAniState1][nowAniState2];
	nowAniCount = 0;
}
void Effect::SetNowMoveState(DWORD ns)
{
	nowMoveState1 = ns;
	nowMoveState2 = 0;
	nowMove = *_Move[nowMoveState1][nowMoveState2];
	nowMoveCount = 0;
}
DWORD Effect::GetNowAniState()
{
	return nowAniState1;
}

void Effect::SetScale(D3DXVECTOR2 nS)
{
	Scalingval = nS;
}
void Effect::SetScaleVel(D3DXVECTOR2 nSV)
{
	Scalingvelo = nSV;
}
void Effect::SetScalelim(D3DXVECTOR2 nSL)
{
	LimitScaling = nSL;
}

void Effect::SetRadian(float nr)
{
	radian = nr;
}
void Effect::SetRadianVel(float nrv)
{
	radianvel = nrv;
}

void Effect::SetNowState(DWORD ns)
{
	nowState = ns;
}

DWORD Effect::GetNowState()
{
	return nowState;
}

void Effect::SetLifeTime(DWORD nl)
{
	limitedTime = nl;
}

DWORD Effect::DeleteCheck()
{
	if(liveTime >= limitedTime)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}